package com.kmxd.ams.app.event.copy.command;

import com.kmxd.ams.client.archive.IArchiveMetaFieldDetailSvc;
import com.kmxd.ams.client.archive.IArchiveMetaFieldMainSvc;
import com.kmxd.ams.client.archive.dto.ArchiveMetaFieldDetailQueryDTO;
import com.kmxd.ams.client.archive.dto.ArchiveMetaFieldMainQueryDTO;
import com.kmxd.ams.client.archive.vo.ArchiveMetaFieldDetailListVO;
import com.kmxd.ams.client.archive.vo.ArchiveMetaFieldMainListVO;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 获取元数据信息
 *
 * @author nickbi
 */
@Slf4j
@Component
@AllArgsConstructor
public class ArchiveMetaFieldCmd {

  private final IArchiveMetaFieldMainSvc archiveMetaFieldMainSvc;
  private final IArchiveMetaFieldDetailSvc archiveMetaFieldDetailSvc;

  public Map<String, String> getMetaFieldMap(String sourceTableId, String targetTableId) {
    if (null == targetTableId) {
      return null;
    }
    List<ArchiveMetaFieldMainListVO> mainList =
        archiveMetaFieldMainSvc.selectList(
            ArchiveMetaFieldMainQueryDTO.builder()
                .sourceTableId(sourceTableId)
                .targetTableId(targetTableId)
                .build());
    if (null == mainList || mainList.isEmpty()) {
      return null;
    }
    ArchiveMetaFieldMainListVO metaFieldMain = mainList.get(0);

    List<ArchiveMetaFieldDetailListVO> detailList =
        archiveMetaFieldDetailSvc.selectList(
            ArchiveMetaFieldDetailQueryDTO.builder().fieldMainId(metaFieldMain.getId()).build());
    if (null == detailList || detailList.isEmpty()) {
      return null;
    }
    return detailList.stream()
        .collect(
            Collectors.toMap(
                ArchiveMetaFieldDetailListVO::getSourceField,
                ArchiveMetaFieldDetailListVO::getTargetField));
  }
}
